package org.dromara.quickswing.ui.app.simple;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import org.dromara.quickswing.ui.app.QSDialog;
import org.dromara.quickswing.ui.app.QSPrefs;
import org.dromara.quickswing.ui.app.QSWidget;

import javax.swing.*;
import java.awt.*;

/**
 * @author Jin
 */
public abstract class QSSimpleDialog<T extends QSWidget<? extends QSPrefs>> extends QSDialog<T> {

    protected QSSimpleDialog(T owner, String title) {
        super(owner, title);
        setupUI(owner);
        initialize(owner);
    }

    protected QSSimpleDialog(T owner, boolean modal) {
        super(owner, modal);
        setupUI(owner);
        initialize(owner);
    }

    protected QSSimpleDialog(T owner, String title, boolean modal) {
        super(owner, title, modal);
        setupUI(owner);
        initialize(owner);
    }


    protected JButton getSubmitButton() {
        return new JButton("OK");
    }

    protected JButton getCancelButton() {
        return new JButton("Cancel");
    }


    protected void setupUI(T owner) {
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
        JPanel footBar = new JPanel();
        footBar.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(footBar, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        footBar.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JPanel toolbarBtnPanel = new JPanel();
        toolbarBtnPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1, true, false));
        footBar.add(toolbarBtnPanel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        JButton buttonOK = getSubmitButton();
        toolbarBtnPanel.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        JButton buttonCancel = getCancelButton();
        toolbarBtnPanel.add(buttonCancel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        JPanel  bodyPanel = getBodyPanel();
        contentPane.add(bodyPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        this.setContentPane(contentPane);
    }

    protected abstract JPanel getBodyPanel();

    protected abstract void initialize(T owner);

}